<?php

namespace backend\models;

/**
 * @author  shuang
 * @date    2016-11-24 19:03:49
 * @version V1.0
 * @desc    
 */
use common\models\TaskReport;
use yii\behaviors\TimestampBehavior;
use common\components\MTools;
use Yii;
use backend\models\MY_Task;
use yii\db\Query;
use frontend\models\WB_UserPayRecord;

class MY_TaskReport extends TaskReport {

    public static $status = [
        1 => "未审核",
        2 => "已忽略",
        3 => "已接受"
    ];

    /*
     * 设置表操作行为动作
     * return array
     */

    public function behaviors() {
        return[
            [
                'class' => TimestampBehavior::className()
            ]
        ];
    }

    /*
     * 配置列表查询数据
     * 条件
     * return object
     */

    public static function getList() {
        $query = new Query();
        $query->select("a.title,a.id,a.qrcode,a.sample_figure,b.reportcount,b.status");
        $query->from("(SELECT COUNT(id) as reportcount,taskid,`status` FROM " . MY_TaskReport::tableName() . "  GROUP BY taskid) as b");
        $query->leftJoin(MY_Task::tableName() . " as a", "b.taskid=a.id");
        $query->andWhere("a.status=67");
        $query->andFilterWhere(["like", "a.title", Yii::$app->request->get("search")]);
        $countQuery = clone $query;
        $offset = Yii::$app->request->get("offset");
        $limit = Yii::$app->request->get("limit");
        $res = $query->offset($offset)->limit($limit)->all();
        return ["total" => $countQuery->count(), "data" => $res];
    }

    /*
     * 插入、更新数据
     * @params $data array
     * @params $id int 主键
     * return boolean 
     */

    public static function createData($id = null) {
        $model = $id ? MY_TaskReport::findOne($id) : new MY_TaskReport();
        try {
            $model->load(Yii::$app->request->post());
            return MTools::saveModel($model);
        } catch (Exception $e) {
            throw $e;
        }
    }

    /*
     * 报告过期审核
     * @params $taskid
     * @params 
     * return boolean
     */

    public static function auditTask($taskid, $pass = true) {
        if ($pass) {
            $taskmodel = MY_Task::findOne($taskid);
            //停止任务
            $trans = Yii::$app->db->beginTransaction();
            try {
                //查看任务已经完成情况
                $comcount = \frontend\models\WB_UserTaskOperate::find()->where("status=1 and taskid=" . $taskid)->count();
                if ($taskmodel->number - $comcount > 0) {
                    $taskmodel->status = 68;
                    $taskmodel->nosheetcount = $taskmodel->number;
                    $taskmodel->number = $comcount;
                    $taskmodel->stopdate = time();
                    $taskmodel->stopcashback = 0;
                    if ($taskmodel->update()) {
                        //检测是否置顶
                        WB_TaskTop::deleteAll("taskid=" . $taskmodel->id);
                        $trans->commit();
                    }
                }
                return true;
            } catch (Exception $ex) {
                $trans->rollBack();
                throw new \yii\web\NotFoundHttpException($ex);
            }
        } else {
            //删除报告任务
            if(MY_TaskReport::deleteAll("taskid=:taskid", [":taskid" => $taskid])){
                return true;
            }
        }
    }

}
